package com.wgchao.authmanager.infrastructure.convert.ev;

import com.wgchao.authmanager.application.vo.AgencyVo;
import com.wgchao.authmanager.infrastructure.convert.Convert;
import com.wgchao.authmanager.infrastructure.jpa.entity.AgencyInfo;
import com.wgchao.authmanager.infrastructure.jpa.entity.UserInfo;
import com.wgchao.authmanager.infrastructure.jpa.repository.UserInfoJpaReposotory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @作者：wgchao
 * @时间：11:06 2020/5/5 0005
 * @用途：
 **/
@Component
public class AgencyInfoConvertVo implements Convert<AgencyVo, AgencyInfo> {
    @Autowired
    private UserInfoJpaReposotory userInfoJpaReposotory;

    @Override
    public AgencyInfo toEntity(AgencyVo agencyVo) {
        return null;
    }

    @Override
    public AgencyVo toModel(AgencyInfo agencyInfo) {
        AgencyVo agencyVo = new AgencyVo();
        BeanUtils.copyProperties(agencyInfo, agencyVo);
        UserInfo creator = userInfoJpaReposotory.findFirstByUnameAndDelFalse(agencyInfo.getCreateUser());
        if(creator != null){
            agencyVo.setCreatorName(creator.getName());
        }
        if(!StringUtils.isEmpty(agencyInfo.getLastUpdateUser())){
            UserInfo updator = userInfoJpaReposotory.findFirstByUnameAndDelFalse(agencyInfo.getLastUpdateUser());
            if(updator != null){
                agencyVo.setLastUpdatorName(updator.getName());
            }
        }
        return agencyVo;
    }
}
